package com.xiehospital.web.controller.system;

import com.xiehospital.common.annotation.Log;
import com.xiehospital.common.core.controller.BaseController;
import com.xiehospital.common.core.domain.AjaxResult;
import com.xiehospital.common.core.page.TableDataInfo;
import com.xiehospital.common.enums.BusinessType;
import com.xiehospital.common.utils.poi.ExcelUtil;
import com.xiehospital.common.core.domain.entity.SysBtype;
import com.xiehospital.system.service.ISysBtypeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 床位类型信息操作处理
 * 
 * @author Xierujian
 */
@Api("床位类型")
@RestController
@RequestMapping("/system/btype")
public class SysBtypeController extends BaseController
{
    @Autowired
    private ISysBtypeService btypeService;

    /**
     * 获取床位类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:btype:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBtype btype)
    {
        startPage();
        List<SysBtype> list = btypeService.selectBtypeList(btype);
        return getDataTable(list);
    }
    
    @Log(title = "床位类型管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:btype:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBtype btype)
    {
        List<SysBtype> list = btypeService.selectBtypeList(btype);
        ExcelUtil<SysBtype> util = new ExcelUtil<SysBtype>(SysBtype.class);
        util.exportExcel(response, list, "床位类型数据");
    }

    /**
     * 根据床位类型编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:btype:query')")
    @GetMapping(value = "/{btypeId}")
    public AjaxResult getInfo(@PathVariable Long btypeId)
    {
        return success(btypeService.selectBtypeById(btypeId));
    }

    /**
     * 新增床位类型
     */
    @PreAuthorize("@ss.hasPermi('system:btype:add')")
    @Log(title = "床位类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysBtype btype)
    {
        if (!btypeService.checkBtypeNameUnique(btype))
        {
            return error("新增床位类型'" + btype.getBtypeName() + "'失败，床位类型名称已存在");
        }
        else if (!btypeService.checkBtypeCodeUnique(btype))
        {
            return error("新增床位类型'" + btype.getBtypeName() + "'失败，床位类型编码已存在");
        }
        btype.setCreateBy(getUsername());
        return toAjax(btypeService.insertBtype(btype));
    }

    /**
     * 修改床位类型
     */
    @PreAuthorize("@ss.hasPermi('system:btype:edit')")
    @Log(title = "床位类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysBtype btype)
    {
        if (!btypeService.checkBtypeNameUnique(btype))
        {
            return error("修改床位类型'" + btype.getBtypeName() + "'失败，床位类型名称已存在");
        }
        else if (!btypeService.checkBtypeCodeUnique(btype))
        {
            return error("修改床位类型'" + btype.getBtypeName() + "'失败，床位类型编码已存在");
        }
        btype.setUpdateBy(getUsername());
        return toAjax(btypeService.updateBtype(btype));
    }

    /**
     * 删除床位类型
     */
    @PreAuthorize("@ss.hasPermi('system:btype:remove')")
    @Log(title = "床位类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{btypeIds}")
    public AjaxResult remove(@PathVariable Long[] btypeIds)
    {
        return toAjax(btypeService.deleteBtypeByIds(btypeIds));
    }

    /**
     * 获取床位类型选择框列表
     */
    @PreAuthorize("@ss.hasPermi('system:btype:optionselect')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysBtype> btypes = btypeService.selectBtypeAll();
        return success(btypes);
    }
}
